//
//  BaseLoginView.swift
//  微博
//
//  Created by mac on 15/12/13.
//  Copyright © 2015ac. All rights reserved.
//

import UIKit

@objc protocol BaseLoginViewDelegate:NSObjectProtocol {
    //登陆
    optional func loginClick()
    //注册
     func registerClick()
    
}


class BaseLoginView: UIView {

    //登陆的代理方法
    weak var delegate:BaseLoginViewDelegate?
    
    //初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        addBaseView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func showBaseView(labelText:String,rutationImage:String?) {
        textLabel.text = labelText
        if let name = rutationImage {
            //做动画
            bigerImage.hidden = true
            rotationView.image = UIImage(named: name)
            backView.hidden = true
        } else {
            loadAnimation()
        }
    }
    
    func loadAnimation() {
        
        let animation = CABasicAnimation()
        animation.keyPath = "transform.rotation"
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.toValue = 2 * M_PI
        animation.removedOnCompletion = false
        rotationView.layer.addAnimation(animation, forKey: nil)
    }
    
    
    func addBaseView() {
        addSubview(rotationView)
        addSubview(backView)
        addSubview(bigerImage)
        addSubview(textLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        
        
        for subView in subviews {
            subView.translatesAutoresizingMaskIntoConstraints = false
        }
        addConstraint(NSLayoutConstraint(item: bigerImage, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: bigerImage, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -60))
        
        //添加圆圈的约束
        addConstraint(NSLayoutConstraint(item: rotationView, attribute: .CenterX, relatedBy: .Equal, toItem: bigerImage, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: rotationView, attribute: .CenterY, relatedBy: .Equal, toItem: bigerImage, attribute: .CenterY, multiplier: 1, constant: 0))
        
        
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: .CenterX, relatedBy: .Equal, toItem: rotationView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: .Top, relatedBy: .Equal, toItem: rotationView, attribute: .Bottom, multiplier: 1, constant: 16))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 230))
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
        
        //按钮的约束
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: textLabel, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: textLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: textLabel, attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: textLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        //设置背景视图的约束 可视化的布局语言
        //OC 中可选项 一般使用 按位 或枚举选项  swift中直接指定数组
        //metrics:约束数值字典 [String: 数值],
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView": backView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-35)-[regBtn]", options: [], metrics: nil, views: ["backView": backView,"regBtn":registerBtn]))
        
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        //监听
        
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)
        
    }
    
    
    /**
    登陆点击事件
    */
    func loginBtnClick() {
        delegate?.loginClick?()
    }
    /**
    注册点击事件
    */
    func registerBtnClick() {
        delegate?.registerClick()
    }
    
    
    
    //懒加载获取所有的属性
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var bigerImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var rotationView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
//    private lazy var :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var textLabel:UILabel = UILabel(content: "关注一些人，回这里看看有什么惊喜,关注一些人，回这里看看有什么惊喜", color: UIColor.darkGrayColor(), fontSize: 14)
    private lazy var loginBtn:UIButton = UIButton(title: "登陆", imageName: nil, color: UIColor.blackColor(), fontSize: 15, backImageName: "common_button_white_disable")
   private lazy var registerBtn:UIButton = UIButton(title: "注册", imageName: nil, color: UIColor.orangeColor(), fontSize: 15, backImageName: "common_button_white_disable")
   
    
}
